<?php

namespace Admin\Controller;

use Admin\Controller\IndexController;
use Zend\Form\Annotation\AnnotationBuilder;
use Admin\Form\StoringBookForm;
use Admin\Model\DBO\StoringBookDBO;
use Admin\Entity\SerialBook;

class StoringDocumentController extends IndexController {

    public function getForm() {
        if (!$this->form) {
            $cataloging = new StoringBookForm();
            $builder = new AnnotationBuilder();
            $this->form = $builder->createForm($cataloging);
        }

        return $this->form;
    }

    public function indexAction() {
        // kiem tra neu chua dang nhap thi quay ve lai trang dang nhap
        $this->checkRole3();
        // Neu dang nhap roi thi hien thi len sach sach nguoi dung
        // Lay thuc the dang nhap dua ra layout
        $identity = $this->getAuthService()->getIdentity();
        $this->layout()->identity = $identity;
        // Hien thi dua sanh sach nguoi dung ra view
        $form = $this->getForm();
        $request = $this->getRequest();
        if ($request->isPost()) {
            $myMessage = array();
            $dataRequest = $request->getPost();
            $form->setData($dataRequest);
            if ($form->isValid()) {
                $data = $form->getData();
                $serialBook = new SerialBook();
                $objectCataloging = new StoringBookDBO($this->getServiceLocator());
                $serialBook->setDataSerialBook($data);
                $serialBook->setPersonCreated($identity);
                $message = $objectCataloging->saveSerialBook($serialBook);
                $myMessage[$message] = $message;
            } else {
                foreach ($form->getMessages() as $value => $ms) {
                    $myMessage[$value] = $value;
                }
            }
            return array(
                'form' => $form,
                'myMessage' => $myMessage,
            );
        }
        return array(
            'form' => $form,
        );
    }

}